package com.commerceerp.system.api.winit;

import com.alibaba.fastjson.JSONObject;
import com.commerceerp.system.api.winit.model.OutboundOrder;
import com.commerceerp.system.api.winit.model.Product;
import com.commerceerp.system.entity.ShopWish;
import com.commerceerp.system.service.FactoryService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.StringUtils;
import net.sf.json.JSONArray;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class WinitUtil
{

    @Autowired
    private BaseHandler baseHandler = new BaseHandler();

    /**
     * 获取派送方式
     * @param warehouseID   仓库id
     */
    public String queryDeliveryWay(String warehouseID) {
        JSONObject data = new JSONObject();
        data.put("warehouseID", warehouseID);
        return baseHandler.doQuery("queryDeliveryWay",data);
    }

    /**
     *
     * 根据产品代码计算每种派送方式的运费
     *
     * @param productCode   产品代码（必填）
     * @param warehouseCode	仓库代码（必填）
     * @param country 国家 （必填）
     * @param region 州
     * @param city 城市 （必填）
     * @param zipCode 邮编 （必填）
     * @param productNum 数量 （必填）
     * @param specification
     * @return
     */
    public String queryDeliveryFee(List<Product> productList, String warehouseCode,String winitProductCode,String country,String region,String city,
    String zipCode,String token) {

        JSONObject data = new JSONObject();
        data.put("country", country);
        data.put("region", region);
        data.put("city", city);
        data.put("productList", productList);
        data.put("warehouseCode", warehouseCode);
        data.put("zipCode", zipCode);
        data.put("winitProductCode", winitProductCode);
        return baseHandler.doQuery("wh.outbound.calcDeliveryFee", data,token);
    }


    /**
     * 获取仓库信息
     */
    public String queryWarehouse() {
        JSONObject data = new JSONObject();
        return baseHandler.doQuery("queryWarehouse",data);
    }

    /**
     * 查询指定万邑通海外仓的库存状态
     */
    public String queryWarehouseStorage(String warehouseID,String token) {
        JSONObject data = new JSONObject();
        data.put("warehouseID",warehouseID);
        return baseHandler.doQuery("queryWarehouseStorage",data,token);
    }

    /**
     * 获取商品信息
     */
    public String queryProductInventoryDetail(String sku,String warehouseId,String token) {
        JSONObject data = new JSONObject();
        data.put("warehouseId",warehouseId);
        data.put("specification",null);
        data.put("productCode",sku);
        return baseHandler.doQuery("queryProductInventoryDetail",data,token);
    }

    //创建出库单
    public String createOutboundOrder(OutboundOrder outboundOrder,JSONArray productList, String token) {
        JSONObject data = new JSONObject();
        data.put("address1",outboundOrder.getAddress1());
        data.put("address2",outboundOrder.getAddress2());
        data.put("city",outboundOrder.getCity());
        data.put("deliveryWayID",outboundOrder.getDeliveryWayID());
        data.put("eBayOrderID",outboundOrder.getEBayOrderID());
        data.put("emailAddress",outboundOrder.getEmailAddress());
        data.put("insuranceTypeID",outboundOrder.getInsuranceTypeID());
        data.put("phoneNum",outboundOrder.getPhoneNum());
        data.put("VATNo",outboundOrder.getVATNo());
        data.put("productList",productList);
        data.put("recipientName",outboundOrder.getRecipientName());
        data.put("region",outboundOrder.getRegion());
        data.put("repeatable","Y");
        data.put("sellerOrderNo",outboundOrder.getSellerOrderNo());
        data.put("state",null);
        data.put("warehouseID",outboundOrder.getWarehouseID());
        data.put("zipCode",outboundOrder.getZipCode());
        data.put("isShareOrder","N");
        data.put("fromBpartnerId","N");
        return baseHandler.doQuery("createOutboundOrder",data,token);
    }

    public static void main(String[] args) {
        WinitUtil w = new WinitUtil();
        w.queryDeliveryWay("1030045");
        //w.queryProductInventoryDetail("YT007_W1C1S5_K1","1054191,1018192,1030045,1051191,1008190,1000008,1022191,1055191,1005189,1000069,1052191,1000001,1000089,1012190,1030042","89435277FA3BA272DE795559998E-");
        //w.QueryProductInventoryList("1054191","YT007_L70M1_X1","89435277FA3BA272DE795559998E-");
//        w.queryDeliveryFee("YT007_W1C1S5_K1","USWC2","US","FL","coral springs","33071"
//        ,"1","","89435277FA3BA272DE795559998E-");
    }


}
